local RoomBase = require 'packages.mahjong.riichi_mahjong.core.roombase'
local ClientPlayer = require "packages.mahjong.riichi_mahjong.client.clientplayer"

---@class RiichiMahjong.Client: RiichiMahjong.RoomBase, ClientBase
local Client = RoomBase:subclass("RiichiMahjong.Client")
Client:include(Fk.Base.ClientBase)

-- 手写泛型 这里帮你写了

---@class RiichiMahjong.Client
---@field public players RiichiMahjong.ClientPlayer[]
---@field public observers RiichiMahjong.ClientPlayer[]
---@field public current RiichiMahjong.ClientPlayer
---@field public center integer @ 上一张打出的麻将牌id
---@field public getPlayerById fun(self: RiichiMahjong.Client, id: integer): RiichiMahjong.ClientPlayer
---@field public getPlayerBySeat fun(self: RiichiMahjong.Client, seat: integer): RiichiMahjong.ClientPlayer
---@field public setCurrent fun(self: RiichiMahjong.Client, current: RiichiMahjong.ClientPlayer)
---@field public getCurrent fun(self: RiichiMahjong.Client): RiichiMahjong.ClientPlayer


function Client:initialize(_client)
  RoomBase.initialize(self)
  Fk.Base.ClientBase.initialize(self, _client)

  self.clientplayer_klass = ClientPlayer
  self.player_tiles = {}

  self:addCallback("RiichiMahjong.DrawTile", self.handleDrawTile, true)
  self:addCallback("RiichiMahjong.SetUp", self.handleSetUp, true)
  self:addCallback("RiichiMahjong.Call", self.handleCall, true)
end

function Client:handleDrawTile(data)
  self:DrawTile(data.t, self:getPlayerById(data.p))
  if Self.id == data.p then
    table.insert(self.player_tiles, data.t)
  end
end


function Client:handleSetUp(data)
  self.player_tiles = data.t
  self.shown_dora_num = 1
  self.dora = {data.d}
end


function Client:handleCall(data)
  local player = self:getPlayerById(data.p)
  player.card_num = player.card_num - #data.m.tiles
  if player == Self then
    for _, tid in ipairs(data.m.tiles) do
      table.removeOne(Client.player_tiles, tid)
    end
  end
  table.insert(player.melds, table.simpleClone(data.m))
end

function Client:sendDataToUI(data)
  -- TODO
end

return Client

